<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"www.lixiaofei.cn","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":true,"storage":true,"lazyload":true,"nav":{"valine":{"order":-1}}},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="无状态的Web应用 参考代码   无状态指的是后台Web应用程序认为每个请求都是独立的，没有前后的逻辑关系，就像一个门卫，看人只看票，谁拿着票都无所谓。   为什么是无状态的呢？这个和互联网的基础架构以及一些历史原因有关，有兴趣可以参考HTTP协议">
<meta property="og:type" content="article">
<meta property="og:title" content="轻量级Django 读书笔记-第二章">
<meta property="og:url" content="http://www.lixiaofei.cn/2018-06-05-django2/index.html">
<meta property="og:site_name" content="管窥程序">
<meta property="og:description" content="无状态的Web应用 参考代码   无状态指的是后台Web应用程序认为每个请求都是独立的，没有前后的逻辑关系，就像一个门卫，看人只看票，谁拿着票都无所谓。   为什么是无状态的呢？这个和互联网的基础架构以及一些历史原因有关，有兴趣可以参考HTTP协议">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2018-06-05T04:23:16.000Z">
<meta property="article:modified_time" content="2020-07-24T07:59:47.973Z">
<meta property="article:author" content="太阳雪">
<meta property="article:tag" content="Django">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://www.lixiaofei.cn/2018-06-05-django2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>轻量级Django 读书笔记-第二章 | 管窥程序</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">管窥程序</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Today is a gift</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/alisx" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://www.lixiaofei.cn/2018-06-05-django2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="太阳雪">
      <meta itemprop="description" content="总结 技术 思考 想法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="管窥程序">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          轻量级Django 读书笔记-第二章
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-06-05 12:23:16" itemprop="dateCreated datePublished" datetime="2018-06-05T12:23:16+08:00">2018-06-05</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-24 15:59:47" itemprop="dateModified" datetime="2020-07-24T15:59:47+08:00">2020-07-24</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Readingnote/" itemprop="url" rel="index"><span itemprop="name">Readingnote</span></a>
                </span>
            </span>

          
            <span id="/2018-06-05-django2/" class="post-meta-item leancloud_visitors" data-flag-title="轻量级Django 读书笔记-第二章" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2018-06-05-django2/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2018-06-05-django2/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1>无状态的Web应用</h1>
<p><a href="https://github.com/alisx/lightdjango_practice/tree/master/Chapter%202" target="_blank" rel="noopener">参考代码</a><br>
  无状态指的是后台Web应用程序认为每个请求都是独立的，没有前后的逻辑关系，就像一个门卫，看人只看票，谁拿着票都无所谓。<br>
  为什么是无状态的呢？这个和互联网的基础架构以及一些历史原因有关，有兴趣可以参考<a href="https://zh.wikipedia.org/wiki/%E8%B6%85%E6%96%87%E6%9C%AC%E4%BC%A0%E8%BE%93%E5%8D%8F%E8%AE%AE" target="_blank" rel="noopener">HTTP协议</a></p>
<a id="more"></a>
<p><strong>无状态的好处</strong></p>
<ul>
<li>易于扩展<br>
将不同的应用可以方便的组织起来</li>
<li>易于创建缓存<br>
不用为不同的状态创建不同缓存，缓存系统易于创建和维护</li>
<li>易于负载均衡<br>
和易于扩增类似，可以通过增加服务器来让Web应用的性能更高，不必考虑有关联的不同请求被分配到不同工作服务器上所带来的问题</li>
</ul>
<p><strong>无状态的应用</strong><br>
  现代的大多数Web应用是要求有状态的，但都是基于这种无状态的结构而来的，服务器端是可以维护状态的，但需要客户端在每次请求时带上特殊的标记，以便服务器客户还原出这个请求的特征，从而判断是否一个相关请求<br>
  对于简单内容展示，产品介绍以及公共信息展示等应用就不必考虑状态问题，做到性能足够好就可以了，这一章的实例中就展示了一个无状态的显示图片的Web应用示例——图片站位服务器</p>
<h2 id="图片占位服务器">图片占位服务器</h2>
<p>  作用很大，对于一个展示网页设计样式的Demo来说，图片占位服务器可以按照请求的要求，生成对应的图片，可以大大提高Demo的制作成本</p>
<h3 id="构件">构件</h3>
<ul>
<li>视图
<ul>
<li>首页视图<br>
展示主要的页面 <code>index</code></li>
<li>图片请求处理视图<br>
按要求处理生成图片，并将图片内容返回 <code>placeholder</code></li>
</ul>
</li>
<li>URL<br>
为每个视图创建一个URL规则<br>
对于图片请求，需要在请求中加入图片的规则，这里只有高和宽，可以自己加上颜色样式等其他信息<br>
URL中的参数<br>
Django2.0用path方法代替url，在url参数设置方面更为简单  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">path(<span class="string">'image/&lt;int:width&gt;x&lt;int:height&gt;/'</span>, placeholder, name=<span class="string">'placeholder'</span>)</span><br></pre></td></tr></table></figure>
表示url中以image开头，后面匹配一个 dddxddd的参数，ddd必须是正整数(对于负数，由于前面要加上负号，就不会被这个url规则匹配)<br>
将匹配好的参数传入到视图 <code>placeholder</code>中，会自动拆分成 <code>width</code>,<code>height</code>  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">placeholder</span><span class="params">(request, width, height)</span>:</span></span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>Django内置的路径转换器类型  参考 <a href="https://www.cnblogs.com/feixuelove1009/p/8399338.html" target="_blank" rel="noopener">Django 2.0 新款URL配置详解</a></p>
</blockquote>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:left">标识</th>
<th style="text-align:left">例子</th>
<th style="text-align:left">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">str</td>
<td style="text-align:left"><code>api/&lt;str:name&gt;</code></td>
<td style="text-align:left">匹配任何非空字符串，但不含斜杠/，如果你没有专门指定转换器，那么这个是默认使用的</td>
</tr>
<tr>
<td style="text-align:left">int</td>
<td style="text-align:left"><code>image/&lt;int:width&gt;</code></td>
<td style="text-align:left">匹配0和正整数，返回一个int类型</td>
</tr>
<tr>
<td style="text-align:left">slug</td>
<td style="text-align:left"><code>api/&lt;slug:slug&gt;</code></td>
<td style="text-align:left">可理解为注释、后缀、附属等概念，是url拖在最后的一部分解释性字符。该转换器匹配任何ASCII字符以及连接符和下划线，比如’ building-your-1st-django-site‘</td>
</tr>
<tr>
<td style="text-align:left">uuid</td>
<td style="text-align:left"><code>api/&lt;uuid:uuid&gt;</code></td>
<td style="text-align:left">匹配一个uuid格式的对象。为了防止冲突，规定必须使用破折号，所有字母必须小写，例如’075194d3-6885-417e-a8a8-6c931e272f00‘ 。返回一个UUID对象</td>
</tr>
<tr>
<td style="text-align:left">path</td>
<td style="text-align:left"><code>file/&lt;path:filepath&gt;</code></td>
<td style="text-align:left">匹配任何非空字符串，重点是可以包含路径分隔符’/‘。这个转换器可以帮助你匹配整个url而不是一段一段的url字符串</td>
</tr>
</tbody>
</table>
<ul>
<li>生成图片
<ul>
<li>利用<a href="https://pillow.readthedocs.io/en/5.1.x/" target="_blank" rel="noopener">Pillow</a>库，生成图片，默认填充为黑色，并且将图片规格画在上面</li>
<li>设置图片格式，和颜色类型(RGB)</li>
<li>返回图片的字节码，这里没有去存储图片，性能会更高，同时为后面的建立图片缓存做好准备</li>
</ul>
</li>
</ul>
<h3 id="请求数据验证">请求数据验证</h3>
<p>只要有人可以参与的流程都需要做验证，人是不靠谱的。验证对程序员来说是个枯燥的苦差事，Django有办法可以帮到你。</p>
<h4 id="1-建立Form验证类"><strong>1.</strong> 建立<code>Form</code>验证类</h4>
<p>Django提供了<code>Form</code>类最为和前端<code>Html</code> <code>form</code>的对应，可以定义字段，并且可以设置字段的类型和数据范围，详细的默认字段可以参考<a href="https://django-chinese-doc.readthedocs.io/zh_CN/latest/ref/models/fields.html#model-field-types" target="_blank" rel="noopener">Django字段类型</a></p>
<h4 id="2-初始化验证实体"><strong>2.</strong> 初始化验证实体</h4>
<p>将来自客户端提交的数据做初始化参数，得到一个<code>form</code>实例</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">form = ImageForm(&#123;<span class="string">'heigth'</span>:heigth, <span class="string">'width'</span>: width&#125;)</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>用这个form实例就可以检验初始化参数是否符合规则了</p>
<blockquote>
<p>例子中，图片的规则是通过<code>GET</code>,用url送来的，所以先要将规则参数从url中解析出来(见上面的 转换器部分),如果通过<code>POST</code>送来的数据初始化from是直接将request.POST作为参数就好了，<a href="https://docs.djangoproject.com/en/2.0/topics/forms/" target="_blank" rel="noopener">参考Django Form</a></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">form = ImageForm(request.POST)</span><br></pre></td></tr></table></figure>
<h4 id="3-利用is-valid-检验数据是否合法"><strong>3.</strong> 利用<code>is_valid()</code>检验数据是否合法</h4>
<p><code>is_valide</code>方法会逐个检查定义在Form中的字段，如果都没问题会返回 <code>True</code>,这时可以用Form实例的<code>cleaned_data</code>属性来获取字段，并且得到的字段类型是经过转换的。如果返回的不是<code>False</code>说明验证失败了，如果需要得到具体的错误，可以冲<code>from.errors</code>中的获取，类似这样的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>form.errors</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">'sender'</span>: [<span class="string">'Enter a valid email address.'</span>], </span><br><span class="line">    <span class="string">'subject'</span>: [<span class="string">'This field is required.'</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>根据校验结果返回合适的response，也可以将验证错误信息返回给前台，这样前台就可以标记哪些字段出了什么错</p>
<h3 id="缓存">缓存</h3>
<p>缓存能很好降低服务器的负荷，提高相应效率</p>
<ul>
<li>
<p>建立缓存机制</p>
<ol>
<li>利用<code>django.core.cache</code>,作为缓存</li>
<li>为每个图片资源创建一个唯一的<code>Key</code>,比如用高宽和文件类型，<code>key = &quot;{}.{}.{}&quot;.format(width, height, image_format)</code></li>
<li>生成图片之前先合成图片的<code>key</code>,在<code>cache</code>中查找，找到即可返回，否则才创建，并且加入到缓存中</li>
</ol>
<blockquote>
<p>缓存是个复杂的话题，涉及到key,命中率，淘汰规则等</p>
</blockquote>
<blockquote>
<p>Django默认使用本地过程、内存缓存，但也可以使用不同的后端(像 Memcached或者文件系统)，需要通过<a href="https://docs.djangoproject.com/en/2.0/topics/cache/" target="_blank" rel="noopener"><code>CACHES</code></a>设置来配置</p>
</blockquote>
</li>
<li>
<p>ETag</p>
</li>
</ul>
<blockquote>
<p>ETag或实体标签（entity tag）是万维网协议HTTP的一部分。 是HTTP协议提供的若干机制中的一种Web缓存验证机制，并且允许客户端进行缓存协商。这就使得缓存变得更加高效，而且节省带宽。如果资源的内容没有发生改变，Web服务器就不需要发送一个完整的响应。<br>
摘自wiki <a href="https://zh.wikipedia.org/wiki/HTTP_ETag" target="_blank" rel="noopener">HTTP ETag</a></p>
</blockquote>
<ol>
<li>定义一个ETag生成方法，方法接收与视图一样的参数</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">generate_etag</span><span class="params">(request, width, height)</span>:</span></span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">return</span> hashlib.sha1(...)</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>为视图加上<code>@etag</code>注解</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@etag(generate_etag)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">placeholder</span><span class="params">(request, width, height)</span>:</span></span><br><span class="line">    ...</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>注意:</strong><br>
书中的例子中，视图定义为<code>function</code>，而在一般情况先视图定义为<code>class</code><br>
对于<code>function</code>可以用注解来加<code>etag</code>,对于<code>class</code>,需要在URL路由规则中加，如：</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在url.py中，如果在views.py模块中定义了 Placeholder 视图类</span></span><br><span class="line"><span class="keyword">from</span> . <span class="keyword">import</span> views</span><br><span class="line">...</span><br><span class="line">path(<span class="string">''</span>, etag(generate_etag)(views.Placeholder.as_view()), name=<span class="string">'placeholder'</span>)</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<h2 id="感悟">感悟</h2>
<ul>
<li>Web应用是建立在无状态的Http协议的基础上的，这个是学习和开发Web应用的基础</li>
<li>Http协议中定义了很多好玩的东西，并且不会因为定义很多影响使用，而是给想进一步的开发者提供更多的便利和支持，例如<code>ETag</code>、<code>last_modified</code></li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Django/" rel="tag"># Django</a>
              <a href="/tags/Python/" rel="tag"># Python</a>
          </div>

        

			<div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/wechat-qcode.png" alt="太阳雪 wechat" style="width: 200px; max-width: 100%;"/>
    <div>关注我，获得更多精彩内容</div>
</div>

        
		
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018-06-01-django1/" rel="prev" title="轻量级Django 读书笔记-第一章">
      <i class="fa fa-chevron-left"></i> 轻量级Django 读书笔记-第一章
    </a></div>
      <div class="post-nav-item">
    <a href="/2018-06-15-django3/" rel="next" title="轻量级Django 读书笔记-第三章">
      轻量级Django 读书笔记-第三章 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#"><span class="nav-number">1.</span> <span class="nav-text">无状态的Web应用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#图片占位服务器"><span class="nav-number">1.1.</span> <span class="nav-text">图片占位服务器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#构件"><span class="nav-number">1.1.1.</span> <span class="nav-text">构件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#请求数据验证"><span class="nav-number">1.1.2.</span> <span class="nav-text">请求数据验证</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-建立Form验证类"><span class="nav-number">1.1.2.1.</span> <span class="nav-text">1. 建立Form验证类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-初始化验证实体"><span class="nav-number">1.1.2.2.</span> <span class="nav-text">2. 初始化验证实体</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-利用is-valid-检验数据是否合法"><span class="nav-number">1.1.2.3.</span> <span class="nav-text">3. 利用is_valid()检验数据是否合法</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#缓存"><span class="nav-number">1.1.3.</span> <span class="nav-text">缓存</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#感悟"><span class="nav-number">1.2.</span> <span class="nav-text">感悟</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="太阳雪"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">太阳雪</p>
  <div class="site-description" itemprop="description">总结 技术 思考 想法</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">88</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">47</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">太阳雪</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>















  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'teRj7yeJj0xpfGKLJ5uFdbIu-gzGzoHsz',
      appKey     : '3uie0p1nxouCCnkjiXTEBeLd',
      placeholder: "您的关注是我的动力",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
